public class ChessBoard {
	ChessPiece[][] board;

	// creates an empty board
	public ChessBoard() {
		board = new ChessPiece[8][8];
	}

	// initializes a board with the pieces in starting position
	public void setupBoard() {
		board = new ChessPiece[8][8];

		// initializes white pieces in 1st row
		board[0][0] = new Rook(ChessGame.BLACK, new BoardLocation(0, 0));
		board[0][1] = new Knight(ChessGame.BLACK, new BoardLocation(0, 1));
		board[0][2] = new Bishop(ChessGame.BLACK, new BoardLocation(0, 2));
		board[0][3] = new Queen(ChessGame.BLACK, new BoardLocation(0, 3));
		board[0][4] = new King(ChessGame.BLACK, new BoardLocation(0, 4));
		board[0][5] = new Bishop(ChessGame.BLACK, new BoardLocation(0, 5));
		board[0][6] = new Knight(ChessGame.BLACK, new BoardLocation(0, 6));
		board[0][7] = new Rook(ChessGame.BLACK, new BoardLocation(0, 7));

		// initializes white pawns in 2nd row
		for (int i = 0; i < 8; i++) {
			board[1][i] = new Pawn(ChessGame.BLACK, new BoardLocation(1, i));
		}

		// initializes black pieces in 8th row
		board[7][0] = new Rook(ChessGame.WHITE, new BoardLocation(7, 0));
		board[7][1] = new Knight(ChessGame.WHITE, new BoardLocation(7, 1));
		board[7][2] = new Bishop(ChessGame.WHITE, new BoardLocation(7, 2));
		board[7][3] = new Queen(ChessGame.WHITE, new BoardLocation(7, 3));
		board[7][4] = new King(ChessGame.WHITE, new BoardLocation(7, 4));
		board[7][5] = new Bishop(ChessGame.WHITE, new BoardLocation(7, 5));
		board[7][6] = new Knight(ChessGame.WHITE, new BoardLocation(7, 6));
		board[7][7] = new Rook(ChessGame.WHITE, new BoardLocation(7, 7));

		// initializes black pawns in 7th row
		for (int i = 0; i < 8; i++) {
			board[6][i] = new Pawn(ChessGame.WHITE, new BoardLocation(6, i));
		}
	}

	// returns the piece at the given location
	public ChessPiece getPiece(BoardLocation location) {
		return board[location.getRow()][location.getCol()];
	}

	// moves a piece from the current location to the given location
	public void move(ChessPiece piece, BoardLocation dest) {
		board[dest.getRow()][dest.getCol()] = piece;
		board[piece.curLoc.getRow()][piece.curLoc.getCol()] = null;
		
		piece.curLoc = dest;
	}

	// prints the whole board
	// prints an underscore for each empty space and calls the toString method
	// for the piece if that space is not empty
	public String toString() {
		String s = "";

		for (int i = 0; i < 8; i++) {
			s += 8 - i + " ";

			for (int j = 0; j < 8; j++) {
				if (getPiece(new BoardLocation(i, j)) != null) {
					s += getPiece(new BoardLocation(i, j)).toString();
				} else {
					s += "_";
				}

				s += " ";
			}

			s += "\n";
		}

		s += "  a b c d e f g h";

		return s;
	}
}
